﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CustomException
{
    public class BusinessException : System.Exception
    {
        #region Private Members

        private List<string> _messages;

        #endregion

        #region Public Properties

        public List<string> Messages
        {
            get { return _messages ?? new List<string>(); }
            set { _messages = value; }
        }

        #endregion

        #region Constructors

        public BusinessException(string message)
            : base(message)
        {
            ExceptionUtils.Log(this);
        }

        public BusinessException(string message, params object[] parameters)
            : base(string.Format(message, parameters))
        {
            ExceptionUtils.Log(this);
        }

        public BusinessException(List<string> messages)
            : base(ConcatListString(messages))
        {
            Messages = messages;
            ExceptionUtils.Log(this);
        }

        public BusinessException(string message, System.Exception ex)
            : base(message, ex)
        {
            ExceptionUtils.Log(this);
        }

        public BusinessException(string message, System.Exception ex, params object[] parameters)
            : base(string.Format(message, parameters), ex)
        {
            ExceptionUtils.Log(this);
        }

        public BusinessException(List<string> messages, System.Exception ex)
            : base(ConcatListString(messages), ex)
        {
            Messages = messages;
            ExceptionUtils.Log(this);
        }

        #endregion

        #region Internal Methods

        private static string ConcatListString(List<string> messages)
        {
            return string.Join(Environment.NewLine, messages.ToArray());
        }

        #endregion
    }
}
